<?php
require_once dirname(__FILE__) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_user_login.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_security.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_roulette.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_matches.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_affiliate.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'html_generator.php';
$title = 'Roulette';

$roulette_id = filter_input(INPUT_GET, "roulette_id");
if (empty($roulette_id)) {
    $roulette_id = filter_input(INPUT_POST, "hidd_roulette_id");
}

    $game_id = filter_input(INPUT_POST, "ddl_game");
    if (empty($game_id)) {
        $game_id = 0;
    }
    $sub_match_id = api_roulette::get_sub_match_id_by_game_id_and_parent_id($game_id, $roulette_id);

    $username_main = $_SESSION['user_info']['username'];
    $user_main = api_security::get_user_by_username($username_main);
    
    $error = "";
    $info = "";
    if (!empty($sub_match_id)) {
        if (!empty($sub_match_id) && $sub_match_id > 0) {
            $choice_id = filter_input(INPUT_POST, "ddl_choice");
            $bet_amount = filter_input(INPUT_POST, "txt_amount");
            if (empty($bet_amount)) {
                $bet_amount = 100; // default
            }
            $affiliate_email = filter_input(INPUT_POST, "txt_affiliate_email");
            if (api_bet_calculators::can_bet($username_main, $bet_amount)) {
                if (!empty($affiliate_email) && filter_var($affiliate_email, FILTER_VALIDATE_EMAIL)) {
                    $info = "Betting Successful with $".$bet_amount.". Proof of bet is sent to your client ($affiliate_email)";
                    api_bet_calculators::place_bet($username_main, $bet_amount, $sub_match_id, $choice_id, $affiliate_email);
                    $affiliate_url = get_affiliate_link($user_main['id'], $affiliate_email);
                    api_affiliate::send_mail_for_affiliate_betting($affiliate_email, $affiliate_url, $bet_amount);
                } else {
                    $info = "Betting Successful with $".$bet_amount.".";
                    api_bet_calculators::place_bet($username_main, $bet_amount, $sub_match_id, $choice_id, "");
                }
            } else {
                $error = "Your balance must be more than $" . $bet_amount;
            }
        } else {
            $sub_match_id = 0;
        }
    }

$disabled = "";
if (!api_matches::is_still_live($roulette_id)) {
    $disabled = "disabled";
}

?>

<!DOCTYPE html>
<!-- IE SECTION -->

<!--[if lt IE 7]>
<html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>
<html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>
<html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js"> <!--<![endif]-->

                     <!-- END IE SECTION -->
<html>
<head>
    <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php' ?>
    
    <script type="text/javascript" >
        var betLoading = new BetAutoLoading(
                1000,
                '<?php echo LINK_ROOT . DIR_AJAX; ?>load_list_bet_placed.php',
                '<?php echo $roulette_id; ?>',
                '#bet_table',
                <?php echo $game_id; ?>,
                '<?php echo LINK_ROOT . DIR_AJAX; ?>loading_profit_if_win.php',
                '#popup_confirm_before_betting_message',
                '#popup_confirm_before_betting',
                'form[name="form_bet"]',
                '<?php echo LINK_ROOT . DIR_AJAX; ?>delete_betting.php');
        var roulette = new Roulette(
                "<?php echo LINK_ROOT . DIR_AJAX; ?>load_choice_options.php",
                "select[name='ddl_game']",
                "select[name='ddl_choice']",
                "<?php echo LINK_ROOT . DIR_AJAX; ?>load_game_name.php",
                "#game_name",
                "<?php echo LINK_ROOT . DIR_AJAX; ?>show_message_end_game.php",
                "#end_game",
                "<?php echo $roulette_id; ?>",
                betLoading);
        var rouletteSpinning = new RouletteSpinning(
                500,
                "<?php echo LINK_ROOT . DIR_AJAX; ?>get_random_roulette_result.php",
                "<?php echo $roulette_id; ?>",
                ".ts-piece-result");
        
        
        $(document).ready(function() {
            betLoading.beginAutoLoad();
            roulette.monitorDdlGame();
            rouletteSpinning.beginAutoLoad();
        });
            
    </script>
</head>
<body>
<?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php' ?>

<!-- Title -->
<div class="section section-black">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="calltoaction-wrapper">
                    <h3>Roulette Detail</h3>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- End Title -->

<!-- Match Detail -->
<div class="section">
    <div class="container">
        <div class="row">

            <div class="col-md-offset-1 col-md-10">
                <div class="service-wrapper row">
                    <form class="form-horizontal" name="form_bet" role="form" method="POST" action="?">
                        
                        
                        <?php $item = api_roulette::get_roulette_by_id($roulette_id); ?>
                        
                        <input type="hidden" name="hidd_roulette_id" value="<?php echo $item['id']; ?>"/>

                        <div class="col-md-3">
                            <img class="text-left" src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES . 'roulette/map.png' ?>" alt="Match" style="height: 500px;">
                        </div>
                        <div class="col-md-offset-1 col-md-8">

                            <div class = "form-group ts-margin-top">
                                <h2 class="col-sm-8" id="game_name"><?php echo api_roulette::get_first_game_name(); ?></h2>
                            </div>
                            
                            <div id="end_game"></div>
                            <?php 
                            show_message($info, $error, 0, 0);
                            ?>
                            
                            <div class="ts-piece-result" style="background-color: black;">36</div>
                            
                            
                            <div class="form-group ts-margin-top">
                                <label class="col-sm-4 control-label">Name</label>

                                <div class="col-sm-4 text-left">
                                    <p class="form-control-static"><?php echo $item['name']; ?></p>
                                </div>
                            </div>
                            <div class="form-group ts-margin-top">
                                <label class="col-sm-4 control-label">Description</label>

                                <div class="col-sm-4 text-left">
                                    <p class="form-control-static"><?php echo $item['description']; ?></p>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-4 control-label">Date Start</label>

                                <div class="col-sm-4 text-left">
                                    <p class="form-control-static"><?php echo show_datetime($item['date_start']); ?></p>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-4 control-label">Date End</label>

                                <div class="col-sm-4 text-left">
                                    <p class="form-control-static"><?php echo show_datetime($item['date_end']); ?></p>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-4 control-label">Remaining Time</label>

                                <div class="col-sm-4 text-left">
                                    <p class="form-control-static ts-red">-
                                                                          <!--2 days, 4 hours, 15 minutes, 10 seconds--></p>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-4 control-label">Game Type</label>

                                <div class="col-sm-4 text-left">
                                    <select name="ddl_game" class="form-control">
                                        <?php show_ddl_game($game_id); ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-4 control-label">Choice</label>

                                <div class="col-sm-4 text-left">
                                    <select name="ddl_choice" class="form-control" <?php echo $disabled; ?>>
                                        <?php show_ddl_game_choice($game_id); ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-4 control-label">Amount</label>

                                <div class="col-sm-4 text-left">
                                    <div class="input-group">
                                        <span class="input-group-addon">$</span>
                                        <input name="txt_amount" type="text" class="form-control" placeholder="100" <?php echo $disabled; ?>>
                                    </div>
                                </div>
                            </div>
                            <?php
                            if (api_matches::is_still_live($roulette_id)) {
                            ?>
                            <input name = "btn_bet" onclick="betLoading.showPopupProfitIfWin();" type = "button" class = "btn" value = "Place Bet">
                            <?php
                            } else {
                            ?>
                            <div class="alert alert-danger">
                                Expired match.
                            </div>
                            <?php
                            }
                            ?>
                        </div>
                    </form>
                </div>
            </div>

        </div>
        <div id="bet_table" class="row">
            
        </div>

    </div>
</div>
<!-- End Match Detail -->

<?php echo show_modal_betting_confirmation("popup_confirm_before_betting", "betLoading.submitBetting();"); ?>

<?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php' ?>
</body>
</html>